$(function () {

    $('#switch_qlogin').click(function () {
        $('#switch_login').removeClass("switch_btn_focus").addClass('switch_btn');
        $('#switch_qlogin').removeClass("switch_btn").addClass('switch_btn_focus');
        $('#switch_bottom').animate({left: '0px', width: '70px'});
        $('#qlogin').css('display', 'none');
        $('#web_qr_login').css('display', 'block');

    });
    $('#switch_login').click(function () {

        $('#switch_login').removeClass("switch_btn").addClass('switch_btn_focus');
        $('#switch_qlogin').removeClass("switch_btn_focus").addClass('switch_btn');
        $('#switch_bottom').animate({left: '154px', width: '70px'});

        $('#qlogin').css('display', 'block');
        $('#web_qr_login').css('display', 'none');
    });
    if (getParam("a") === '0') {
        $('#switch_login').trigger('click');
    }

});

function logintab() {
    scrollTo(0);
    $('#switch_qlogin').removeClass("switch_btn_focus").addClass('switch_btn');
    $('#switch_login').removeClass("switch_btn").addClass('switch_btn_focus');
    $('#switch_bottom').animate({left: '154px', width: '96px'});
    $('#qlogin').css('display', 'none');
    $('#web_qr_login').css('display', 'block');

}

//根据参数名获得该参数 pname等于想要的参数名
function getParam(pname) {
    var params = location.search.substr(1); // 获取参数 平且去掉？ 
    var ArrParam = params.split('&');
    if (ArrParam.length === 1) {
        //只有一个参数的情况 
        return params.split('=')[1];
    } else {
        //多个参数参数的情况
        for (var i = 0; i < ArrParam.length; i++) {
            if (ArrParam[i].split('=')[0] === pname) {
                return ArrParam[i].split('=')[1];
            }
        }
    }
}

var pwdmin = 6;
$(document).ready(function () {
    var contextPath = getContextPath();
    //点击登录
   /* $('#log').click(function (){
        var user_longin = $('#user_longin');
        var login_err = $('#login_err')
        var username = $('#user_longin').val();
        var password = $('#log_pwd').val();
        if (username === "" || password ==="") {
            user_longin.focus().css({
                border: "1px solid red",
                boxShadow: "0 0 2px red"
            });
            login_err.text("×用户名和密码不能为空");
            return false;
        } else {
            login_err.text("");
            user_longin.css({
                border: "1px solid #D7D7D7",
                boxShadow: "none"
            });
        }
        $.post({
            url: contextPath + '/user/userLoginVerify',
            data: {username: username, password: password},
            success: function (msg) {
                if (msg !== "ok") {
                     login_err.text(msg);
                }else {
                   $('#log_user').submit();
                }
            },
        });
    });*/

    //注册简单验证
    $('#user').blur(() => {  //用户名输入框失去焦点
        //用户名位4-16字符
        var username = $('#user').val();
        if (username.length < 4 || username.length > 16 || $.trim(username) === "") {
            $('#userCue #err').text("×用户名为4-16字符");
            $('#user').css({
                border: "1px solid red",
                boxShadow: "0 0 2px red"
            })
        } else {
            $('#userCue #err').text('');
            $('#user').css({
                border: "1px solid #D7D7D7",
                boxShadow: "none"
            });
            $.post({
                url: contextPath + "/user/isUsernameExist",
                data: {username: username},
                success: function (msg) {
                    $('#userCue #err').text(msg);
                },
            });
        }
    })

    //点击注册
    $('#reg').click(function () {
        if ($('#user').val() === "") {
            $('#user').focus().css({
                border: "1px solid red",
                boxShadow: "0 0 2px red"
            });
            $('#userCue #err').text("×用户名不能为空");
            return false;
        } else {
            $('#user').css({
                border: "1px solid #D7D7D7",
                boxShadow: "none"
            });
        }
        if ($('#user').val().length < 4 || $('#user').val().length > 16) {
            $('#user').focus().css({
                border: "1px solid red",
                boxShadow: "0 0 2px red"
            });
            $('#userCue #err').text("×用户名位4-16字符");
            return false;
        } else {
            $('#user').css({
                border: "1px solid #D7D7D7",
                boxShadow: "none"
            });

        }

        if ($('#passwd').val().length < pwdmin) {
            $('#passwd').focus();
            $('#userCue #err').text("×密码不能小于" + pwdmin + "位");
            return false;
        }
        if ($('#passwd2').val() !== $('#passwd').val()) {
            $('#passwd2').focus();
            $('#userCue #err').text("×两次密码不一致！");
            return false;
        }
        if ($('#authCode').val().length !== 4 || $('#authCode').val() === "") {
            $('#authCode').focus();
            $('#userCue #err').text("×请输入4位验证码!");
            return false;
        } else {
            $.post({
                url: contextPath + '/user/userRegisterVerify',
                data: {authCode: $('#authCode').val()},
                success: (msg) => {
                    if (msg !== "ok"){
                        $('#userCue #err').text(msg);
                    }else {
                        $('#reg_user').submit();
                    }
                }
            })
        }

    });


});

async function refreshCode() {
    let src = await $('#captcha_img').attr('src') + "?time=" + new Date().getTime()
    await $("#captcha_img").prop("src", src)
}

function getContextPath() {
    let pathName = document.location.pathname;
    let index = pathName.substring(1).indexOf("/");
    let result = pathName.substring(0, index + 1);
    return result;
}

